<template>
  <div class="account-data">
    <el-scrollbar style="height:90%">
      <div class="add-box">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addAcount">新增账号</el-button>
      </div>
      <el-table :data="tableData" border style="width:100%" class="table-box">
        <el-table-column label="角色" width="120" prop="role">
          <template slot-scope="scope">
            <el-select
              @change="selectChange(scope.row)"
              v-if="scope.row.edit"
              v-model="scope.row.role"
            >
              <el-option
                v-for="option in options"
                :label="option.role"
                :value="option.role"
                :key="option.key"
              ></el-option>
            </el-select>
            <span v-else>
              <span v-if="scope.row.role==='超级管理员'" style="color:#19be6b">超级管理员</span>
              <span v-else-if="scope.row.role==='管理员' " style="color:#FF9900">管理员</span>
              <span v-else-if="scope.row.role==='游客'" style="color:#2db7f5">游客</span>
              <span v-else style="color:#515a6e">客服</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="账号" width="120" prop="username">
          <template slot-scope="scope">
            <el-input v-model="scope.row.username" v-if="scope.row.edit"></el-input>
            <span v-else>{{scope.row.username}}</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="des"></el-table-column>
        <!-- <el-table-column label="创建时间" prop="date"></el-table-column> -->
        <el-table-column label="操作" width="120">
          <template slot-scope="scope" v-if="scope.row.username != 'admin'">
            <el-button
              v-if="!scope.row.edit"
              @click="handleEdit(scope.$index,scope.row)"
              size="mini"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              v-else
              @click="handleSave(scope.$index,scope.row)"
              size="mini"
              icon="el-icon-finished"
              type="success"
            ></el-button>
            <el-button
              @click="handleDelete(scope.$index,scope.row)"
              size="mini"
              icon="el-icon-delete"
              type="danger"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>
    <AddAccount
      :options="options"
      @update="getData"
      :dialogVisible="dialogVisible"
      @closeDialog="closeDialog"
    ></AddAccount>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Provide } from "vue-property-decorator";
import { State, Getter, Mutation, Action } from "vuex-class";
import AddAccount from "./AddAccount.vue";
@Component({
  components: {
    AddAccount
  }
})
export default class AccountData extends Vue {;
  @Provide() dialogVisible: Boolean = false; // 是否展示编辑页面
  @Provide() tableData: any = []; // 表格数据
  // select数据
  @Provide() options: any = [
    {
      key: "admin",
      role: "管理员",
      des: "Super Administrator. Have access to view all pages."
    },
    {
      key: "editor",
      role: "客服",
      des: "Normal Editor. Can see all pages except permission page"
    },
    {
      key: "visitor",
      role: "游客",
      des: "Just a visitor. Can only see the home page and the document page"
    }
  ];
  handleEdit(index: number, row: any) {
    console.log(row);
    // 编辑
    row.edit = true;
  }
  handleSave(index: number, row: any) {
    // 编辑
    // console.log(row);
    //设置 edit 为 false
    row.edit = false;
    //调取接口 传递进去这行的数据 因为在点击完成之前我们就已经对数据做了改变
    (this as any).$axios
      .post(`/api/users/editUser/${row._id}`, row)
      .then((res: any) => {
        (this as any).$message({
          message: res.data.msg,
          type: "success"
        });
      });
  }
  handleDelete(index: number, row: any): void {
    // 删除
    (this as any).$axios
      .delete(`/api/users/deleteUser/${row._id}`)
      .then((res: any) => {
        (this as any).$message({
          message: res.data.msg,
          type: "success"
        });

        this.tableData.splice(index, 1);
      });
  }
  created() {
    this.getData();
  }
  // loadData() {
  //   (this as any).$axios(`/api/users/allUsers`).then((res: any) => {
  //     console.log(res.data);
  //     this.tableData = res.data.datas ? res.data.datas : [];
  //   });
  // }

  addAcount(): void {
    this.dialogVisible = true;
  }
  closeDialog() {
    this.dialogVisible = false;
  }
  getData() {
    (this as any).$axios("/api/users/allUsers").then((res: any) => {
      // 设置编辑状态
      res.data.datas.forEach((item: any) => {
        item.edit = false;
      });
      this.tableData = res.data.datas;
    });
  }
  selectChange(value: any) {
    console.log(value);
    this.options.map((item:any) => {
      if (item.role == value.role) {
        value.key = item.key;
        value.des = item.des;
      }
    });
  }
}
</script>

<style lang="scss" scoped>
.account-data {
  height: 100%;
  overflow: auto;
  .add-box {
    margin-bottom: 10px;
  }
}
</style>
